package com.zpdhy.util;

import com.zpdhy.system.model.UserInfo;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.Md5Hash;

/**
 * Created by kedong on 2017/1/1.
 */
public class EndecryptUtils {

    /**
     * 对密码进行md5加密,并返回密文和salt，包含在User对象中
     *
     * @param username 用户名
     * @param password 密码
     * @return 密文和salt
     */
    public static UserInfo md5Password(String username, String password) {
        SecureRandomNumberGenerator secureRandomNumberGenerator = new SecureRandomNumberGenerator();
        String salt = secureRandomNumberGenerator.nextBytes().toHex();
        //组合username,两次迭代，对密码进行加密
        String password_cipherText = new Md5Hash(password, username + salt, 2).toHex();
        UserInfo user = new UserInfo();
        user.setPassword(password_cipherText);
        user.setSalt(salt);
        user.setUserName(username);
        return user;
    }
}
